package com.ms.game.rebate.db;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;

import com.ms.game.common.container.BasicSettingServerBinUtil;
import com.ms.game.rebate.RebateCoinData;
import com.ms.game.rebate.RebateType;
import com.ms.game.rebate.message.SRebateCoinDataMessage;
import com.ms.game.rebate.message.SRebateData;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.datacenterlite.CentralAsyncCommand;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.message.MessageAction;

public class CheckHasRebateDCLAsync extends CentralAsyncCommand {

	private static final long serialVersionUID = -4617368132550742203L;
	
	public GUID playerID; 
	
	public GUID sessionID;
	
	private ArrayList<RebateModel> uplineRewardRebates;
	
	private HashMap<Date,RebateCoinData> commisonRebates;
	
	@Override
	public void asyncExecute() throws Exception {
		commisonRebates = RebateDAO.checkCommisonRebateData(playerID);
		uplineRewardRebates = RebateDAO.checkUplineInviteRewardData(playerID);
	}

	@Override
	public void callBackExecute(Object... userData) throws Exception {
		RebateCoinData monthlyFeeDailyAwardRebate = (RebateCoinData)userData[1];
		RebateCoinData weeklyFeeDailyAwardRebate = (RebateCoinData)userData[2];

		SRebateCoinDataMessage rebateCoinDataMessage = new SRebateCoinDataMessage();
		if (commisonRebates != null && commisonRebates.size() > 0) {
			ArrayList<SRebateData> sRebateDatas = new ArrayList<SRebateData>();
			for (RebateCoinData rebateCoinData : commisonRebates.values()) {
				SRebateData commisonData = new SRebateData();
				commisonData.type = RebateType.COMMISSION.getId();
				commisonData.belongsDate = rebateCoinData.getEndDate();//client要結束時間，因為不改資料結構，所以
				commisonData.rebateCoins = rebateCoinData.getRebateCoins().toString();
				sRebateDatas.add(commisonData);
			}
			// 把有的抽佣加入陣列
			rebateCoinDataMessage.commisonRebates = sRebateDatas;
		}

		if (monthlyFeeDailyAwardRebate != null) {
			if (monthlyFeeDailyAwardRebate.isSuccess()) {
				SRebateData monthlyFeeData = new SRebateData();
				monthlyFeeData.type = RebateType.MONTHLY_FEE_DAILY_PAY.getId();
				monthlyFeeData.belongsDate = monthlyFeeDailyAwardRebate.getBelongsDate();
				monthlyFeeData.rebateCoins = monthlyFeeDailyAwardRebate.getRebateCoins().toString();
				rebateCoinDataMessage.monthlyFeeDailyAwardRebate = monthlyFeeData;
			}
		}
		
		if (weeklyFeeDailyAwardRebate != null) {
			if (weeklyFeeDailyAwardRebate.isSuccess()) {
				SRebateData weeklyFeeData = new SRebateData();
				weeklyFeeData.type = RebateType.WEEKLY_FEE_DAILY_PAY.getId();
				weeklyFeeData.belongsDate = weeklyFeeDailyAwardRebate.getBelongsDate();
				weeklyFeeData.rebateCoins = weeklyFeeDailyAwardRebate.getRebateCoins().toString();
				rebateCoinDataMessage.weeklyFeeDailyAwardRebate = weeklyFeeData;
			}
		}
		
		if(uplineRewardRebates !=null){
			ArrayList<SRebateData> sRebateDatas = new ArrayList<SRebateData>();
			for (RebateModel model : uplineRewardRebates) {
				SRebateData uplineRewardData = new SRebateData();
				uplineRewardData.type = RebateType.UPLINE_INVITE_AWARD.getId();
				uplineRewardData.belongsDate = model.getEndDate().getTime();
				uplineRewardData.downloadId = model.getDownlineId();
				uplineRewardData.rebateCoins = model.getTotalRebateCoin().toString();
				sRebateDatas.add(uplineRewardData);
			}
			rebateCoinDataMessage.uplineInviteRewardRebate = sRebateDatas;
		}
		
		//取得距離現在最近的更新時間
		ArrayList<Long> timeList = new ArrayList<Long>();
		for (String timeFormat : BasicSettingServerBinUtil.bin.getEventFlsuhTime().values()) {
			timeList.add(GameUtility.getEventNextFlushTime(timeFormat).getTime());
		}
		//因為時間不確定，所以排序一下把第一筆送過去
		Collections.sort(timeList);
		
		
		rebateCoinDataMessage.nextCheckTime = timeList.get(0);
		rebateCoinDataMessage.errorCode = ErrorCode.SUCCESS;
		MessageAction.sendMessage(sessionID, MessageTypes.S_CHECK_HAS_REBATE_RESPONSE, rebateCoinDataMessage);
	}

}
